package com.springBoot.springBootSysCore.modules.repository.system;

import com.springBoot.springBootSysCore.common.repository.IBaseRepository;
import com.springBoot.springBootSysCore.modules.entity.system.SysRole;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 资源
 * @author timo
 *
 */
public interface SysRoleRepository extends IBaseRepository<SysRole, String>{
    @Query("from SysRole  r  left join r.users as u where u.id=?1 ")
    public List<SysRole> findByUserId(String id);

    public SysRole findByName(String name);
    @Query("select r.name from SysRole r")
    public List<String> findAllName();
    /**
     * 根据用户名查询角色code --- 非默认角色
     * @param account
     * @return
     */
    @Query("select r.code from SysRole r  left join r.users as u where u.account = ?1 and u.delFlag = '0'")
    public List<String> findCodeByUserAccount(String account);
    /**
     * 根据用户id查询角色id -- 非默认角色
     * @param userId
     * @return
     */
    @Query("select role.id from SysRole role left join role.users as us where us.id = ?1")
    public List<String> findRoleIdByUser(String userId);

    public SysRole findByCode(String code);
    /**
     * 根据id查询角色
     * @param roleIds
     * @return
     */
    @Query("from SysRole where id in (?1)")
    public List<SysRole>  getRoleList(List<String> roleIds);
    /**
     * 根据资源Id查询拥有该资源的角色----资源删除验证用
     * @author PSC
     * @param ids
     * @return
     */
    @Query("select t from SysRole t inner join t.resources r where r.id in(?1)")
    public List<SysRole> findByResourceIds(List<String> ids );
    /**
     * 批量删除角色
     * @author PSC
     * @param asList
     */
    @Modifying
    @Query("delete from SysRole where id in (?1)")
    public void deleteRolesById(List<String> asList);

    @Query("select o.id from SysRole o where o.name = ?1")
    public List<String> queryIdByName(String name);
}
